package yt.demo.demoadmin.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import yt.demo.appcommonbean.http.DeleteRequest;
import yt.demo.appcommonbean.http.RestResponse;
import yt.demo.appcommondb.base.BaseEntity;
import yt.demo.appcommondb.dto.SplitPageDTO;
import yt.demo.appcommondb.vo.PageVO;
import yt.demo.appcoresystem.entity.SystemDept;
import yt.demo.appcoresystem.service.ISystemDeptService;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController extends BaseController {

    private final ISystemDeptService service;

    @Autowired
    public DeptController(ISystemDeptService service) {
        this.service = service;
    }

    @GetMapping("/getPageList")
    public RestResponse<PageVO<SystemDept>> getPageList(SplitPageDTO page, SystemDept params) {
        return RestResponse.ok(this.service.getPageList(page, params, w -> w.orderByDesc(SystemDept::getId), null));
    }

    @GetMapping("/getList")
    public RestResponse<List<SystemDept>> getList(SystemDept params) {
        return RestResponse.ok(this.service.getList(params, w -> w.orderByDesc(SystemDept::getId), null));
    }

    @PostMapping("/create")
    public RestResponse<SystemDept> create(@RequestBody @Validated(BaseEntity.CreateGroup.class) SystemDept req) {
        return RestResponse.ok(this.service.create(req));
    }

    @PostMapping("/update")
    public RestResponse<SystemDept> update(@RequestBody @Validated(BaseEntity.UpdateGroup.class) SystemDept req) {
        return RestResponse.ok(this.service.update(req));
    }

    @PostMapping("/delete")
    public RestResponse<Void> delete(@RequestBody DeleteRequest req) {
        this.service.delete(req);
        return RestResponse.ok();
    }

}
